package com.wxp.demo05.button;

import android.os.Bundle;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.Switch;
import android.widget.TextView;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import com.wxp.demo05.R;

/**
 * 2.2 开关按钮Switch
 */
public class SwitchActivity extends AppCompatActivity implements CompoundButton.OnCheckedChangeListener {

    private TextView tv_state1;
    private TextView tv_state2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_switch);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });
        this.setTitle("2.2 开关按钮Switch");

        Switch sw_state1 = this.findViewById(R.id.sw_state1);
        tv_state1 = this.findViewById(R.id.tv_state1);
        CheckBox sw_state2 = this.findViewById(R.id.sw_state2);
        tv_state2 = this.findViewById(R.id.tv_state2);
        //设置事件
        sw_state1.setOnCheckedChangeListener(this);
        sw_state2.setOnCheckedChangeListener(this);
    }

    @Override
    public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
        String selectStateText = compoundButton.getContext().getString(R.string.select_state);
        String stateText = b ? "开" : "关";
        if (compoundButton.getId() == R.id.sw_state1) {
            tv_state1.setText(selectStateText + stateText);
        } else if (compoundButton.getId() == R.id.sw_state2) {
            tv_state2.setText(selectStateText + stateText);
        }
    }
}